package cn.iocoder.yudao.module.course.content.service.coursecontent;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.course.content.controller.admin.coursecontent.vo.*;
import cn.iocoder.yudao.module.course.content.dal.dataobject.coursecontent.CourseContentDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.course.content.convert.coursecontent.CourseContentConvert;
import cn.iocoder.yudao.module.course.content.dal.mysql.coursecontent.CourseContentMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.course.content.enums.ErrorCodeConstants.*;

/**
 * 课时内容管理 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class CourseContentServiceImpl implements CourseContentService {

    @Resource
    private CourseContentMapper courseContentMapper;

    @Override
    public Long createCourseContent(CourseContentCreateReqVO createReqVO) {
        // 插入
        CourseContentDO courseContent = CourseContentConvert.INSTANCE.convert(createReqVO);
        courseContentMapper.insert(courseContent);
        // 返回
        return courseContent.getId();
    }

    @Override
    public void updateCourseContent(CourseContentUpdateReqVO updateReqVO) {
        // 校验存在
        validateCourseContentExists(updateReqVO.getId());
        // 更新
        CourseContentDO updateObj = CourseContentConvert.INSTANCE.convert(updateReqVO);
        courseContentMapper.updateById(updateObj);
    }

    @Override
    public void deleteCourseContent(Long id) {
        // 校验存在
        validateCourseContentExists(id);
        // 删除
        courseContentMapper.deleteById(id);
    }

    private void validateCourseContentExists(Long id) {
        if (courseContentMapper.selectById(id) == null) {
            throw exception(COURSE_CONTENT_NOT_EXISTS);
        }
    }

    @Override
    public CourseContentDO getCourseContent(Long id) {
        return courseContentMapper.selectById(id);
    }

    @Override
    public List<CourseContentDO> getCourseContentList(Collection<Long> ids) {
        return courseContentMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<CourseContentDO> getCourseContentPage(CourseContentPageReqVO pageReqVO) {
        return courseContentMapper.selectPage(pageReqVO);
    }

    @Override
    public List<CourseContentDO> getCourseContentList(CourseContentExportReqVO exportReqVO) {
        return courseContentMapper.selectList(exportReqVO);
    }

}
